package com.jhgsys.internal.peccancy.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.peccancy.constant.PeccancyConstant;
import com.jhgsys.internal.peccancy.entity.PeccancyClause;
import com.jhgsys.internal.peccancy.mapper.PeccancyClauseMapper;
import com.jhgsys.internal.peccancy.service.IPeccancyClauseService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 违章条款类型 Service实现
 *
 * @author admin
 * @date 2024-08-08 13:20:26
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PeccancyClauseServiceImpl extends ServiceImpl<PeccancyClauseMapper, PeccancyClause> implements IPeccancyClauseService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-08 13:20:26
     * @param request QueryRequest
     * @param peccancyClause peccancyClause
     * @return IPage<PeccancyClause>
    */
    @Override
    public IPage<PeccancyClause> findPeccancyClausePage(QueryRequest request, PeccancyClause peccancyClause) {
        LambdaQueryWrapper<PeccancyClause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyClause::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(peccancyClause.getClauseName())){
            queryWrapper.like(PeccancyClause::getClauseName,peccancyClause.getClauseName());
        }
        if(StringUtils.isNotBlank(peccancyClause.getTenantId())){
            queryWrapper.eq(PeccancyClause::getTenantId,peccancyClause.getTenantId());
        }
        if(StringUtils.isNotBlank(peccancyClause.getStatus())){
            queryWrapper.eq(PeccancyClause::getStatus,peccancyClause.getStatus());
        }
        if(StringUtils.isNotBlank(peccancyClause.getClauseName())){
            queryWrapper.like(PeccancyClause::getClauseName,peccancyClause.getClauseName());
        }
        if(StringUtils.isNotBlank(peccancyClause.getUseScope())){
            queryWrapper.and(item->item.eq(PeccancyClause::getUseScope,peccancyClause.getUseScope()).or().eq(PeccancyClause::getUseScope, PeccancyConstant.USE_SCOPE.ALL));
        }
        queryWrapper.orderByAsc(PeccancyClause::getSort);

        Page<PeccancyClause> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-08 13:20:26
     * @param peccancyClause peccancyClause
     * @return List<PeccancyClause>
     */
    @Override
    public List<PeccancyClause> findPeccancyClauseRuleList(PeccancyClause peccancyClause) {
	    LambdaQueryWrapper<PeccancyClause> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PeccancyClause::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(peccancyClause.getTenantId())){
            queryWrapper.eq(PeccancyClause::getTenantId, peccancyClause.getTenantId());
        }
        if(StringUtils.isNotBlank(peccancyClause.getStatus())){
            queryWrapper.eq(PeccancyClause::getStatus, peccancyClause.getStatus());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-08 13:20:26
    * @param id
    * @return PeccancyClause
    */
    @Override
    public PeccancyClause findPeccancyClauseById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-08 13:20:26
     * @param peccancyClause peccancyClause
     */
    @Override
    @Transactional
    public void createPeccancyClause(PeccancyClause peccancyClause) {
        peccancyClause.setCreatedTime(new Date());
        this.save(peccancyClause);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-08 13:20:26
     * @param peccancyClause peccancyClause
     */
    @Override
    @Transactional
    public void updatePeccancyClause(PeccancyClause peccancyClause) {
        peccancyClause.setUpdatedTime(new Date());
        this.saveOrUpdate(peccancyClause);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-08 13:20:26
     * @param peccancyClause peccancyClause
     * @param peccancyClauseIds peccancyClauseIds
     */
    @Override
    @Transactional
    public void deletePeccancyClause(PeccancyClause peccancyClause,String peccancyClauseIds) {
        List<String> list = Arrays.asList(peccancyClauseIds.split(StringPool.COMMA));
        this.baseMapper.update(peccancyClause,new LambdaQueryWrapper<PeccancyClause>().in(PeccancyClause::getClauseId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-08 13:20:26
    * @param peccancyClauseIds peccancyClauseIds
    */
    @Override
    @Transactional
    public void physicallyDeletePeccancyClause(String peccancyClauseIds) {
        List<String> list = Arrays.asList(peccancyClauseIds.split(StringPool.COMMA));
        LambdaQueryWrapper<PeccancyClause> wapper = new LambdaQueryWrapper<>();
        wapper.in(PeccancyClause::getClauseId,list);
        this.remove(wapper);
    }
}
